package cn.hxlc.tie.demo.modular.zr.material.service.impl;/*
 @author rui
 @date 2021/7/15 --14:01
*/



import cn.hutool.core.bean.BeanUtil;
import cn.hxlc.tie.demo.modular.zr.material.entity.Material;
import cn.hxlc.tie.demo.modular.zr.material.entity.MaterialGroup;
import cn.hxlc.tie.demo.modular.zr.material.mapper.MaterialMapper;
import cn.hxlc.tie.demo.modular.zr.material.model.params.MaterialParam;
import cn.hxlc.tie.demo.modular.zr.material.model.result.MaterialResult;
import cn.hxlc.tie.demo.modular.zr.material.service.MaterialService;
import cn.hxlc.tie.model.page.PageResult;
import cn.hxlc.tie.libs.mp.page.PageFactory;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;

@Service
@Transactional
public class MaterialServiceImpl extends ServiceImpl<MaterialMapper, Material> implements MaterialService {

    @Autowired
    private MaterialMapper materialMapper;

    private Serializable getKey(MaterialParam param){
        return param.getFid();
    }
    private Material getEntity(MaterialParam param){
       Material entity=new Material();
        BeanUtil.copyProperties(param,entity);
        return entity;
    }
    private Material getOldEntity(MaterialParam param){
        return this.getById(getKey(param));
    }
    @Override
    public PageResult<MaterialResult> queryListPage(MaterialParam materialParam) {
        Page<MaterialResult> materialPage= PageFactory.defaultPage();
        IPage<MaterialResult> resultPage= materialMapper.queryListPage(materialPage,materialParam);
        return new PageResult<>(resultPage);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addFirst(MaterialParam materialParam) {
        Material entity=new Material();
        BeanUtil.copyProperties(materialParam,entity);
        this.baseMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateOne(MaterialParam materialParam) {
        Material oldEntity = getOldEntity(materialParam);
        Material newEntity = getEntity(materialParam);
        BeanUtil.copyProperties(newEntity, oldEntity);
        this.baseMapper.updateById(oldEntity);

    }

    @Override
    public void deleteOne(Long fid) {
      this.baseMapper.deleteById(fid);
    }

}
